import easygui as g
import os

def data(path):
    list1=list(os.walk(path))
    for wd in range(len(list1)):
        for document in list1[wd][2]:
            flag=os.path.splitext(document)[1]
            if flag in list2:
                print('正在分析文件:%s'%document)
                count1=line(document)
                try :
                    dirt1[flag]+=1
                except KeyError:
                    dirt1[flag] = 1
                try:
                    dirt2[flag]+=count1
                except KeyError:
                    dirt2[flag]=count1

def line(document):
    count1 =0
    with open(document,'r',encoding='utf-8') as f1:
        try :
            for each_line in f1:
                count1 +=1
        except UnicodeDecodeError:
            pass
    return count1
                    
def show(dirt2):
    count=0
    lines=0
    内容 = ''
    for i in dirt1.keys():
        lines=dirt2[i]
        count+=lines
        进度 = str(count/1000)+'%'
        剩余 = 100000-count
        内容 += '【%s】文件共%d个，代码%d行\n' %(i,dirt1[i],lines)
    msg = '您目前共累积编写了%d行代码，完成进度%s\n离10万行代码\
还差%d行，请继续努力'% (count,进度,剩余)
    g.textbox(msg,'统计结果',内容)
path=g.diropenbox()
count=0
dirt1={}
dirt2={}
list2=['.c', '.cpp', '.py', '.cc', '.java', '.pas', '.asm']
data(path)
show(dirt2)



